﻿namespace DimensionsHelper.Client.WinUi.Helpers;

internal class EnumDescriptionHelper
{
    private static readonly Dictionary<Type, Dictionary<string, int>> DescriptionCache = [];

    public static string[] GetDescriptions(Type type)
    {
        if (!DescriptionCache.TryGetValue(type, out Dictionary<string, int>? cache))
        {
            cache = TypeHelper.ReadEnumMembersDescription(type);
            DescriptionCache.Add(type, cache);
        }

        return cache.Keys.ToArray();
    }

    public static int GetValueOfDescription(Type type, string description)
    {
        if (DescriptionCache.TryGetValue(type, out Dictionary<string, int>? cache) &&
            cache.TryGetValue(description, out int value))
        {
            return value;
        }

        return 0;
    }


    public static string GetDescriptionOfValue(Type type, int value)
    {
        if (DescriptionCache.TryGetValue(type, out Dictionary<string, int>? cache))
        {
            return cache.FirstOrDefault(p => p.Value == value).Key;
        }

        return string.Empty;
    }
}